<?php
namespace app\index\controller;

use app\index\model\AuthGroup;
use app\index\model\AuthRule;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Auth extends Controller{
    function __construct(Request $request)
    {
        parent::__construct($request);
        Session::init();
        if(Session::get('name')!=1){
            $this->error('请先登录',url('index/Login/login','',''));
        }
        //权限
        $uid = Session::get('id');

        $auth = new \think\auth\Auth();
        $rule = $this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action();

        $result = $auth->check($rule, $uid);
        if($result==false){
            $this->error('没有访问权限','index/Index/info');
        }

    }
    public function authlist(){
        \think\Config::set('url_common_param', true);
        $request=Request::instance();
        $title=$request->get('title','','trim');
        $data=[];
        //URL
        if(''!==$title){
            $data['title']=$title;
        }
        $this->assign('title',$title);

        $list=AuthRule::Authlist($data,$request->get());
        $this->assign('list',$list);

        return $this->fetch();
    }

    public function huishou_authlist(){
        \think\Config::set('url_common_param', true);
        $request=Request::instance();
        $title=$request->get('title','','trim');
        $data=[];
        //URL
        if(''!==$title){
            $data['title']=$title;
        }
        $this->assign('title',$title);

        $list=AuthRule::huishouAuthlist($data,$request->get());
        $this->assign('list',$list);

        return $this->fetch();
    }

    public function edit_authlist(){
        $request=Request::instance();
        $id=$request->get('id');
        $this->assign('id',$id);


        if($request->isPost()){
            $post=$request->post();
            $userObj=new AuthRule();
            $result=$userObj->edit_Authlist($post);

            if(false===$result){
                $this->error('修改失败'.$userObj->getError());
            }elseif(0===$result){
                $this->error('没有修改任何数据');
            }else{

                $this->success('修改成功',url('index/Auth/Authlist'));
            }

        }else{
            $id=$request->param('id','','');
            if(empty($id)){
                $this->error('参数异常,没接到id');
            }
            $userObj=AuthRule::get($id);

            $result=[];
            if (!is_null($userObj)) {
                $result = $userObj->toArray();
            } else {
                $this->error('参数错误');
            }
            $this->assign($result);
            return $this->fetch();
        }
    }
    public function del_auth(){
        $request=Request::instance();
        $id = $this->request->param('id');
        if($request->isGet() && !empty($id)){
            $ret = \app\index\model\AuthRule::destroy($id);
            if($ret){  $this->success('删除成功',url('index/Auth/authlist'));
            } else{
                $this->error('删除失败');
            }
        }
        return $this->fetch();
    }
    //物理删除
    public function dell(){

        $id = $this->request->param('id');
        if($this->request->isGet() && !empty($id)){
            $ret = \app\index\model\AuthRule::destroy($id,true);
            if($ret){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
    }

    public function huifu_authlist(){
        $id=Request::instance()->param('id');
        if($id){
            $up=Db::table('app_auth_rule')->where('id',$id)->update(['del_time'=>Null]);
            if($up){
                $this->success('恢复成功');
            }else{
                $this->error('恢复失败');
            }
        }else{
            $this->error('没有id值');
        }
    }
    public function add(){
        $request=Request::instance();
        if($request->isPost()){
            $post=$request->post();
            $rt=new AuthRule();
            $ret=$rt->addlist($post);
            if (false === $ret) {
                $this->error('新增失败!' . $rt->getError());
            } else {
                $this->success('新增成功!', 'index/Auth/authlist');
            }
        }
        return $this->fetch();
    }

    public function group(){
        $request=Request::instance();
        $title=$request->get('title','','trim');
        $data=[];

        if(''!==$title){
            $data['title']=$title;
        }
        $this->assign('title',$title);

        $list=AuthGroup::grouplist($data,$request->get());
        $this->assign('list',$list);

        return $this->fetch();
    }
    public function huishou_group(){
        \think\Config::set('url_common_param', true);
        $request=Request::instance();
        $title=$request->get('title','','trim');
        $data=[];

        if(''!==$title){
            $data['title']=$title;
        }
        $this->assign('title',$title);

        $list=AuthGroup::huishouGroup($data,$request->get());
        $this->assign('list',$list);

        return $this->fetch();
    }

    public function addgroup(){
        $request=Request::instance();

        if($request->isPost()){
            $post=$request->post();
            $ru=$request->post('rules/a',[],'');

            $post['rules']=implode(',',$ru);

            $rt=new AuthGroup();
            $ret=$rt->addlist($post);
            if (false === $ret) {
                $this->error('新增失败!' . $rt->getError());
            } else {
                $this->success('新增成功!', 'index/Auth/group');
            }
        }
        $list=AuthRule::alist();
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function edit_group(){
        \think\Config::set('url_common_param', true);
        $request=Request::instance();
        $id=$request->get('id');
        $this->assign('id',$id);


        if($request->isPost()){
            $post=$request->post();
            $ru=$request->post('rules/a',[],'');
            $post['rules']=implode(',',$ru);
            $userObj=new AuthGroup();
            $result=$userObj->edit_group($post);

            if(false===$result){
                $this->error('修改失败'.$userObj->getError());
            }elseif(0===$result){
                $this->error('没有修改任何数据');
            }else{

                $this->success('修改成功',url('index/Auth/group'));
            }

        }else{
            $id=$request->param('id','','');
            if(empty($id)){
                $this->error('参数异常,没接到id');
            }
            $userObj=AuthGroup::get($id);
            $list=AuthRule::alist();
            $this->assign('list',$list);
            $result=[];
            if (!is_null($userObj)) {
                $result = $userObj->toArray();
            } else {
                $this->error('参数错误');
            }
            $this->assign($result);
            //权限打对号

            $tiao=AuthGroup::get($id);
            $gou=explode(',',$tiao['rules']);
            $this->assign('gou',$gou);


            return $this->fetch();
        }
    }

    public function del_group(){
        $request=Request::instance();
        $id = $this->request->param('id');
        if($request->isGet() && !empty($id)){
            $ret = \app\index\model\AuthGroup::destroy($id);
            if($ret){  $this->success('删除成功',url('index/Auth/group'));
            } else{
                $this->error('删除失败');
            }
        }
        return $this->fetch();
    }

    public function huifu_group(){
        $id=Request::instance()->param('id');
        if($id){
            $up=Db::table('app_auth_group')->where('id',$id)->update(['del_time'=>Null]);
            if($up){
                $this->success('恢复成功');
            }else{
                $this->error('恢复失败');
            }
        }else{
            $this->error('没有id值');
        }
    }
    //物理删除
    public function delll(){

        $id = $this->request->param('id');
        if($this->request->isGet() && !empty($id)){
            $ret = \app\index\model\AuthGroup::destroy($id,true);
            if($ret){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
    }

}